package com.atguigu.gmall.mq.config;

/**
 * @author: Mamba
 * @version: 1.0
 * @Time: 2021/1/12 20:01
 * @Description: Configuration相当于之前的<bean class="类" id="类的id"></bean> application.xml
 * 使用：
 * @Autoward
 * private 类型 id
 *      MQ 服务器中如果没有指定队列 才会重新创建  如果有了 不创建
 */

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class DeadLetterMqConfig {
    public static final String exchange_dead = "exchange.dead";//普通交换机 死信交换机
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";//TTL
    public static final String queue_dead_2 = "queue.dead.2";


    //队列 普通
    @Bean
    public Queue queue2(){
        return QueueBuilder.durable(queue_dead_2).build();
    }

    //队列 TTL
    @Bean
    public Queue queue1(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", exchange_dead);
        arguments.put("x-dead-letter-routing-key",routing_dead_2);
        //局部设置的TTL 优先级高于全局设置的TTL
        arguments.put("x-message-ttl",10*1000);//全局设置  过期时间
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();
    }
    //交换机 普通交换机
    @Bean
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }

    //
    @Bean
    public Binding binding1(){
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1).noargs();
    }

    @Bean
    public Binding binding2(){
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2).noargs();
    }
}
